<!-- 
 # @Date     : 2024/10/02
 # @Author   : RedHerring
 # @File     : admin.vue
 # @帮客 : AI Freedom
 # @撰写      : 左右 管理员审核页面
-->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['system:gl:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:gl:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:gl:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['system:gl:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="glList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="学生姓名" align="center" prop="moralityStdName" />
            <el-table-column label="学生学号" align="center" prop="moralityStdId" />
            <el-table-column label="项目" align="center" prop="moralityWord" />
            <el-table-column label="分数" align="center" prop="moralityAward" />
            <el-table-column label="描述" align="center" prop="moralityDescribe" />
            <el-table-column label="类型" align="center" prop="moralityType">
                <template #default="scope">
                    <span v-if="scope.row.moralityType == '0'">加分</span>
                    <span v-if="scope.row.moralityType == '1'">扣分</span>
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="moralityAwait">
                <template #default="scope">
                    <span v-if="scope.row.moralityAwait == 0">待审核</span>
                    <span v-if="scope.row.moralityAwait == 1">审核通过</span>
                    <span v-if="scope.row.moralityAwait == 2">审核失败</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="审核状态(0加分 1减分)" align="center" prop="moralityType" /> -->
            <!-- <el-table-column label="总决策" align="center" prop="moralityDecision" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:gl:edit']">修改</el-button>
                    <el-button link type="primary" icon="Edit" @click="getCheck(scope.row)"
                        v-hasPermi="['system:gl:check']">审核</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:gl:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改品德（详情）对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body draggable>
            <el-form ref="glRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="学生姓名" prop="moralityStdName">
                    <el-input v-model="form.moralityStdName" placeholder="请输入学生姓名" disabled />
                </el-form-item>
                <el-form-item label="学生学号" prop="moralityStdId">
                    <el-input v-model="form.moralityStdId" placeholder="请输入学生学号" disabled />
                </el-form-item>

                <el-form-item label="类型" prop="moralityType">
                    <el-select v-model="form.moralityType" class="m-2" placeholder="请选择类型" :disabled="power">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="项目" prop="moralityWord" v-show="form.moralityType != null">
                    <el-select v-model="form.moralityWord" class="m-2" placeholder="请选择项目" @change="getDataList"
                        :disabled="power">
                        <el-option v-for="item in dataList" :key="item.dictLabel" :label="item.dictLabel"
                            :value="item.dictLabel" />
                    </el-select>
                </el-form-item>
                <el-form-item label="分数" prop="moralityAward">
                    <el-input v-model="form.moralityAward" placeholder="" disabled />
                </el-form-item>
                <el-form-item label="描述" prop="moralityDescribe">
                    <el-input v-model="form.moralityDescribe" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"
                        :disabled="power" placeholder="请输入描述" />
                </el-form-item>

                <el-form-item label="审核" prop="moralityAwait">
                    <el-select v-model="form.moralityAwait" class="m-2" placeholder="请选择类型">
                        <el-option v-for="item in options_await" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Gl">
import { listGl, getGl, delGl, addGl, updateGl } from "@/api/system/gl";
// 导入字典数据
import { listData } from "@/api/system/dict/data";

// const router = useRouter();

const route = useRoute();
const { proxy } = getCurrentInstance();

// 字典数据列表
const dataList = ref([]);

const glList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 判断是修改还是审核
const power = ref(false);

// 类型
const options = [
    {
        value: '0',
        label: '奖励',
    },
    {
        value: '1',
        label: '扣分',
    },
]


// 审核类型
const options_await = [
    {
        value: '0',
        label: '待审核',

    },
    {
        value: '1',
        label: '通过',

    },
    {
        value: '2',
        label: '拒绝',

    },
]




const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        moralityStdName: null,
        moralityStdId: null,
        moralityStdTotal: null,
        moralityAward: null,
        moralityWord: null,
        moralityDescribe: null,
        moralityImg: null,
        moralityAwait: null,
        moralityType: null,
        moralityDecision: null
    },
    rules: {
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询品德（详情）列表 */
function getList() {
    loading.value = true;
    listGl(queryParams.value).then(response => {
        glList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        moralityId: null,
        moralityStdName: null,
        moralityStdId: null,
        moralityStdTotal: null,
        moralityAward: null,
        moralityWord: null,
        moralityDescribe: null,
        moralityImg: null,
        moralityAwait: null,
        moralityType: null,
        moralityDecision: null
    };
    proxy.resetForm("glRef");
}



/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.moralityId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加品德（详情）";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    power.value = false;

    reset();
    const _moralityId = row.moralityId || ids.value
    getGl(_moralityId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改品德（详情）";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["glRef"].validate(valid => {
        if (valid) {
            if (form.value.moralityId != null) {
                updateGl(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addGl(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _moralityIds = row.moralityId || ids.value;
    proxy.$modal.confirm('是否确认删除品德（详情）编号为"' + _moralityIds + '"的数据项？').then(function () {
        return delGl(_moralityIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('system/gl/export', {
        ...queryParams.value
    }, `gl_${new Date().getTime()}.xlsx`)
}

/** 查询字典数据列表 */
function getList_zd() {
    loading.value = true;
    listData({ dictType: 'sys_quanti_s', pageNum: 1, pageSize: 1000, remark: '品德表现' }).then(response => {
        dataList.value = response.rows;
        loading.value = false;
    });
}

function getDataList(val) {
    const itemObj = dataList.value.find((item) => {
        return item.dictLabel === val;
    });
    form.value.moralityAward = itemObj.dictValue;
}



// 审核
function getCheck(row) {
    power.value = true;

    reset();
    const _moralityId = row.moralityId || ids.value
    getGl(_moralityId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "审核品德（详情）";
        power.value = true;
    });
}


getList_zd();
getList();
</script>